package com.pro.core.aspect;

import com.alibaba.fastjson.JSON;
import com.pro.core.aspect.log.SystemLog;
import com.pro.core.util.AopUtils;
import com.pro.core.util.WebUtils;
import com.pro.db.domain.ProLog;
import com.pro.db.service.itf.IProLogService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
public class SystemLogAspect {
    private final Log logger = LogFactory.getLog(SystemLogAspect.class);

    @Autowired
    IProLogService logService;

    @Pointcut("@annotation(com.pro.core.aspect.log.SystemLog)")
    public void pointcut(){}

    @Around(value = "pointcut()")
    public Object handle(ProceedingJoinPoint jp) throws Throwable{
       /* String invokeClass = joinPoint.getTarget().getClass().getName();
        logger.info("系统操作日志后置切面，目标类：" + invokeClass);

        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        // 入参value
        Method method = ms.getMethod();

        // 方法的注解对象
        SystemLog anno = method.getAnnotation(SystemLog.class);
        OpTypeEnum opType = anno.operType();
        ModuleEnum moduleName = anno.module();*/

        Object result = AopUtils.executeJoinPointMethod(jp, jp.getArgs());
        if (result!=null) {
            ProLog lo = getLogger(jp);
            String reqStr = AopUtils.getRequestString(jp);
            lo.setRequestString(reqStr);
            lo.setResponseString(JSON.toJSONString(result));
            logService.save(lo);
        }
        return result;
    }


    private ProLog getLogger(ProceedingJoinPoint jp){
        //获取切入点方法及参数
        Method method = AopUtils.getMethod(jp);
        SystemLog systemLog = AopUtils.getMethodAnnotation(method, SystemLog.class);
       /* try {
            Boolean flag = PropertiesCacheUtil.loadProjectProperties(AdminPropertiesConfig.LOGGER_CONFIG).getBoolean(t.type().toString());
            //未配置或true都记录日志,仅为false时不记录
            if (flag!=null&&!flag) {
                return null;
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        ProLog logOperater = new ProLog();
        logOperater.setAddTime(LocalDateTime.now());
        logOperater.setModule(systemLog.module().getDescription());
        logOperater.setAction(systemLog.action().getDescription());
        logOperater.setComment(method.getName());
        if(null != WebUtils.getCurrentUser()){
            logOperater.setAdminId(WebUtils.getCurrentUser().getId());
            logOperater.setAdminName(WebUtils.getCurrentUser().getUsername());
        }
        return logOperater;
    }

}
